<?php

class Trackcomments_model extends Base_Model
{
    const TABLE = 'mod_track_comments';

    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 获取评论列表信息
     *
     * @param $where
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function getCommentsList($whereData, $offset, $limit)
    {
        $where = ' where 1';
        if(isset($whereData['uid'])) 
        	$where .= ' AND a.uid = '.$whereData['uid'];
        //已评价的订单
        if($whereData['iscomment'] == 1){
        	$where .= ' AND a.status = 7 ';
            $where .= ' AND b.id is not null ';
        }
        //待评价的订单
        else{
        	$where .= ' AND a.status = 99 ';
            $where .= ' AND b.id is null ';
        }
        $limit = ' limit '.$offset.', '.$limit;

        $sql = <<<EOT
SELECT
	a.id as order_id,
	c.name,
	c.thumb,
	b.track_score,
    b.tourist_pic,
	b.created,
	b.comments
FROM
	app_order a
LEFT JOIN (
	SELECT
		id,
		name,
		thumb,
		type
	FROM
		mod_activity
	UNION ALL
		SELECT
			id,
			name,
			thumb,
			type
		FROM
			mod_track
	UNION ALL
		SELECT
			id,
			name,
			thumb,
			type
		FROM
			mod_ziying
) c ON c.id = a.goodsId
AND c.type = a.goodsType
LEFT JOIN mod_track_comments b ON a.orderNo = b.oid
EOT;
        $sql .= $where.$limit;
        $query = $this->db->query($sql)->result_array();
        foreach ($query as &$v){
			if(!empty($v['tourist_pic'])){
			    $v['tourist_pic']=explode(';', rtrim(trim($v['tourist_pic']),';'));
				foreach ($v['tourist_pic'] as &$pic){
					$pic=$this->img($pic);
				}
			}				
			if(!empty($v['created'])) 
				$v['created']=date('Y-m-d H:i', $v['created']);
		    if(!empty($v['thumb']))
				$v['thumb']=$this->img($v['thumb']);
			if(!empty($v['track_score']))
				$v['track_score']=$this->img('/resource/www/imgs/score_'.$v['track_score'].'.png');
		}
        return $query;
    }

    /**
     * 获取指定评论
     *
     * @param $where
     * @return array
     */
    public function getComments($where)
    {
        return $this->db->get_where(self::TABLE, $where)->row_array();

    }
    /**
     * 获取景点评论
     *
     * @param $where
     * @return array
     */
    public function getSenicComments($where)
    {
    	return $this->db->get_where('mod_scenic_comments', $where)->row_array();
    
    }

    /**
     * 添加评论
     *
     * @param $data
     */
    public function addComments($data)
    {
        return $this->db->insert(self::TABLE, $data);
    }
    
    /**
     * 添加景点评论
     *
     * @param $data
     */
    public function addSenicComments($data)
    {
    	return $this->db->insert('mod_scenic_comments', $data);
    }
    /**
     * 获取线路平均得分
     *
     * @param $data
     */
    public function getAvgScore($where)
    {
    	$this->db->select_avg('track_score','avg_score');
    	$this->db->where($where);
    	return $this->db->get(self::TABLE)->row_array();
    }
    /**
     * 更新线路平均得分
     *
     * @param $data
     */
    public function updateAvgScore($data,$table,$where)
    {
    	$this->db->set($data);
        $this->db->where($where);
        return $this->db->update($table);
    }

    
}
